<?php if(isset($listingId) && $listingId) { ?>
<div class="action">
	<?php echo $html->link(__("Add Deal"), array("action"=>"admin_add", $listingId), array("class"=>"add_button")); ?>
</div>
<?php } ?>
<div class=" index">
<div class="clear"></div>
<?php
echo "<div class='switcher'>";
echo __("Switch to ").$html->link("Deals Settings",array("plugin"=>"deals","controller"=>"deal_config","action"=>"setting","Deal"));
echo "</div><br/>";


$index->setData($deals, 'Deal');
$index->showFields(array('id','title',array('list_category_id'=>'Category'),array('listing_id'=>'Listing'),array('regular_price'=>'Regular Price'),array('discount_type'=>'Discount Type'),array('discount'=>'Discount'),array('published_on'=>'Published'),array('expires'=>'Valid until'),array('deal_status'=>'Status')));
//$index->sortableFields(array());
$index->addColumn('listing_id','showListing');
$index->addColumn('list_category_id','showCategory');
$index->addColumn('discount','showDiscount');
//$index->addColumn('deal_status','showDealStatus');
$index->addColumn('published_on','showPublishedOn');

function showListing($record)
{
	$html = ClassRegistry::getObject('view')->loaded['html'];
	return $html->link($record['Listing']['title'],array('plugin'=>null,'controller'=>'listings','action'=>'admin_view',$record['Listing']['id']));
}
function showCategory($record)
{
	$html = ClassRegistry::getObject('view')->loaded['html'];
	return $html->link($record['ListCategory']['name'],array('plugin'=>null, 'controller'=>'categories','action'=>'admin_view',$record['ListCategory']['id']));
}
function showDiscount($record)
{
	return $record['Deal']['discount'].(($record['Deal']['discount_type'])?" %":"");
}
/*
function showDealStatus($record)
{
	return $record['Deal']['dealStatus'];
}
*/
function showPublishedOn($record)
{
	return date("Y-m-d", strtotime($record['Deal']['publish_on_date']))." ".$record['Deal']['publish_on_time'];
}
/*
$index->addColumn('discount_type','showDiscountType');
function showDiscountType($record)
{
	$discountTypes = ClassRegistry::getObject('view')->viewVars['discountTypes'];
	return $discountTypes[$record['Deal']['discount_type']];
}
*/


function publish_unpublish($model,$record)
{
	if($record[$model]['status'] == Deal::PUBLISH)
	{
		$action['title'] = __('Unpublish');
		$action['url'] = '/admin/deals/deals/changeStatus/'.$record[$model]['id'];
		$action['confirmMessage'] = __('Are you sure, you want to unpublish this deal ?',true);
	} else {
		$action['title'] = __('Publish');
		$action['url'] = '/admin/deals/deals/changeStatus/'.$record[$model]['id'];
	}
	return $action;
}

/*
function toggle_purchase_link($model, $record)
{
	if($record[$model]['deal_type'] == Deal::TYPE_PROMOTION)
	{
		if($record[$model]['purchase_link_status'])
		{
			$action['title'] = __('Deactivate Online Purchase');
			$action['url'] = '/admin/deals/deals/changePurchaseLinkStatus/'.$record[$model]['id'];
			$action['confirmMessage'] = __('Are you sure ?');
		} else {
			$action['title'] = __('Activate Online Purchase');
			$action['url'] = '/admin/deals/deals/changePurchaseLinkStatus/'.$record[$model]['id'];
		}
		return $action;
	} else {
		return false;
	}
}
*/

$index->setAction(__('View'), array('action'=>'view', ':Deal.id'));
$index->setAction(__('Edit'), array('action'=>'edit', ':Deal.id'));
$index->setAction(__('Delete'), array('action'=>'delete', ':Deal.id'),$this->helpers,__('Are you sure, you want to delete this record ?'));
$index->setAction(null, null,$this->helpers,false,true,'publish_unpublish');
//$index->setAction(null, null,$this->helpers,false,true,'toggle_purchase_link');

$index->setAction(__("Show purchased"), array("controller"=>"deal_orders","action"=>"admin_index",":Deal.id"));

$index->addButton(__("Delete Selected"),array("action"=>"bulkDelete",'admin'=>true,'plugin'=>'deals',"controller"=>"deals"),array("onSubmit"=>"if(!confirm('".__("Are you sure?")."')){return false;}"));
$index->addButton(__("Unpublish Selected"),array("action"=>"bulkUnpublish",'admin'=>true,"plugin"=>"deals","controller"=>"deals"));
$index->addButton(__("Publish Selected"),array("action"=>"bulkPublish",'admin'=>true,"plugin"=>"deals","controller"=>"deals"));


echo $index->display($deals);
?>
</div>